<template>
    <div>
        <div>个人中心</div>
        <el-button @click="getPaperList">得到试卷</el-button>
        <el-table :data="paperList">
            <el-table-column prop="title" label="试卷名称"></el-table-column>
            <el-table-column prop="courseName" label="课程"></el-table-column>
            <el-table-column prop="specialty" label="专业"></el-table-column>
            <el-table-column prop="teacherName" label="出卷教师"></el-table-column>
            <el-table-column label="考试总时间">
                <template slot-scope="scope">
                    {{ scope.row.totalTime }} 分钟
                    <!-- <span style="margin-left: 10px">{{ scope.row.date }}</span> -->
                </template>
            </el-table-column>
            <!-- <el-table-column
                prop="totalTime"
                label="考试总时间"
               >
            </el-table-column>-->
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-link
                        type="primary"
                        @click="startExamBtnClick(scope.$index, scope.row)"
                        v-loading.fullscreen.lock="fullscreenLoading"
                    >开始考试</el-link>
                </template>
            </el-table-column>
        </el-table>
    </div>
</template>

<script>
import { Promise } from "q";
export default {
    data() {
        return {
            classId: sessionStorage.classId,
            paperList: [],
            fullscreenLoading: false
        };
    },
    created() {
        this.getPaperList();
    },
    methods: {
        getPaperList() {
            this.$http("api/paper/list", {
                // params: { classId: this.classId }
            }).then(({ data }) => {
                // console.log("data", data);
                this.paperList = data.data;
                console.log("this.paperList", this.paperList);
            });
        },
        startExamBtnClick(index, row) {
            // this.$confirm("是否开始考试", "提示", {
            //     confirmButtonText: "确定",
            //     cancelButtonText: "取消",
            //     type: "primary"
            // }).then(() => {
            //     // this.fullscreenLoading = true;
            //     console.log("index, row", index, row);
            // });
            this.fullscreenLoading = true;
            let content = JSON.parse(row.content);

            let fixedTest = [];
            let randomTest = [];
            let _promise = [];
            // 得到固定试题
            if (JSON.stringify(content.fixed) !== "{}") {
                let test = this.$http("api/paper/getFixedTest", {
                    params: {
                        content: content.fixed
                    }
                });
                _promise.push(test);
            }

            // 得到随机试题
            if (JSON.stringify(content.random) !== "{}") {
                let test = this.$http.post("api/getRandomTest", content.random);
                _promise.push(test);
            }
            let test = {};
            Promise.all(_promise)
                .then(values => {
                    console.log('values', values)
                    values.map(item => {
                        for (let type in item.data.data) {
                            if (!test[type]) test[type] = [];
                            test[type] =  test[type].concat(item.data.data[type]);
                        }
                    });
                    console.log('考试试题内容test', test)
                    this.fullscreenLoading = false;
                })
                .catch(err => {
                    this.fullscreenLoading = false;
                });
        }
    }
};
</script>


